

    
    //
    use self::time::Duration;
    use sdl2::pixels::Color;
    use std::{thread, time};
    //   
    pub struct BACV{
        
    }
     impl BACV{ 
       pub fn ByteArray__TO__HexString(&mut self) {
           
       }
       
       
       pub fn String_utf8__TO__ByteArray(s:String)->Vec<u8>{
           let mut v:Vec<u8>=Vec::with_capacity(s.as_bytes().len());
           v=vec![100;s.as_bytes().len() as usize];

           for i in 0 ..s.as_bytes().len(){
               v[i]=s.as_bytes()[i];
               //v.push(s.as_bytes()[i]);
           }   
           
           return v;
       }
        pub fn ByteArray__TO__String_utf8(v:Vec<u8>)->String{
          let  s=std::str::from_utf8(&v).unwrap().to_string();
         return   s;
       }
       ////////////


       pub fn ByteArray__TO__i32(ba:Vec<u8>)->i32{
        //ba.to_ne_bytes();
       let mut e:[u8;4]=[0;4];
        for t in 0 ..4{
            e[t]=ba[t];
        }
       let i:i32=  i32::from_le_bytes(e);
       return i; 
    }
    pub fn i32__TO__ByteArray(i:i32)->Vec<u8>{
      // return i.to_ne_bytes().to_owned().unwrap();;
        return i.to_ne_bytes() .iter().cloned().collect();
        //×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
    }
    
       pub  fn String_i32__TO__i32(s:String)->i32{ 
        return  s.parse::<i32>().unwrap();
       }
       pub  fn i32__TO__String(i:i32)->String{ 
        //let mut s:String="".to_string();
        let s=i.to_string();
        return s;  
       }
 ////////////


 pub fn ByteArray__TO__i64(ba:Vec<u8>)->i64{
    //ba.to_ne_bytes();
   let mut e:[u8;8]=[0;8];
    for t in 0 ..8{
        e[t]=ba[t];
    }
   let i:i64=  i64::from_le_bytes(e);
   return i; 
}
pub fn i64__TO__ByteArray(i:i64)->Vec<u8>{
  // return i.to_ne_bytes().to_owned().unwrap();;
    return i.to_ne_bytes() .iter().cloned().collect();
    //×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
}

   pub  fn String_i64__TO__i64(s:String)->i64{ 
    return  s.parse::<i64>().unwrap();
   }
   pub  fn i64__TO__String(i:i64)->String{ 
    //let mut s:String="".to_string();
    let s=i.to_string();
    return s;  
   }

   
   /////////////


   pub fn ByteArray__TO__i16(ba:Vec<u8>)->i16{
    //ba.to_ne_bytes();
   let mut e:[u8;2]=[0;2];
    for t in 0 ..2{
        e[t]=ba[t];
    }
   let i:i16=  i16::from_le_bytes(e);
   return i; 
}
pub fn i16__TO__ByteArray(i:i16)->Vec<u8>{
  // return i.to_ne_bytes().to_owned().unwrap();;
    return i.to_ne_bytes() .iter().cloned().collect();
    //×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
}

    pub fn String_i16__TO__i16(s:String)->i16{ 
    return  s.parse::<i16>().unwrap();
   }
    pub fn i16__TO__String(i:i16)->String{ 
    //let mut s:String="".to_string();
    let s=i.to_string();
    return s;  
   }
   ///////////////////


   pub fn ByteArray__TO__i8(ba:Vec<u8>)->i8{
    //ba.to_ne_bytes();
   let mut e:[u8;1]=[0;1];
    for t in 0 ..1{
        e[t]=ba[t];
    }
   let i:i8=  i8::from_le_bytes(e);
   return i; 
}
pub fn i8__TO__ByteArray(i:i8)->Vec<u8>{
  // return i.to_ne_bytes().to_owned().unwrap();;
    return i.to_ne_bytes() .iter().cloned().collect();
    //×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
}

   pub  fn String_i8__TO__i8(s:String)->i8{ 
    return  s.parse::<i8>().unwrap();
   }
   pub  fn i8__TO__String(i:i8)->String{ 
    //let mut s:String="".to_string();
    let s=i.to_string();
    return s;  
   }

  //////////////////////uuuuu

  pub fn ByteArray__TO__u32(ba:Vec<u8>)->u32{
    //ba.to_ne_bytes();
   let mut e:[u8;4]=[0;4];
    for t in 0 ..4{
        e[t]=ba[t];
    }
   let i:u32=  u32::from_le_bytes(e);
   return i; 
}
pub fn u32__TO__ByteArray(i:u32)->Vec<u8>{
  // return i.to_ne_bytes().to_owned().unwrap();;
    return i.to_ne_bytes() .iter().cloned().collect();
    //×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
}

    pub fn String_u32__TO__u32(s:String)->u32{ 
    return  s.parse::<u32>().unwrap();
   }
    pub fn u32__TO__String(i:u32)->String{ 
    //let mut s:String="".to_string();
    let s=i.to_string();
    return s;  
   }
////////////


pub fn ByteArray__TO__u64(ba:Vec<u8>)->u64{
//ba.to_ne_bytes();
let mut e:[u8;8]=[0;8];
for t in 0 ..8{
    e[t]=ba[t];
}
let i:u64=  u64::from_le_bytes(e);
return i; 
}
pub fn u64__TO__ByteArray(i:u64)->Vec<u8>{
// return i.to_ne_bytes().to_owned().unwrap();;
return i.to_ne_bytes() .iter().cloned().collect();
//×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
}

pub fn String_u64__TO__u64(s:String)->u64{ 
return  s.parse::<u64>().unwrap();
}
pub fn u64__TO__String(i:u64)->String{ 
//let mut s:String="".to_string();
let s=i.to_string();
return s;  
}


/////////////


pub fn ByteArray__TO__u16(ba:Vec<u8>)->u16{
//ba.to_ne_bytes();
let mut e:[u8;2]=[0;2];
for t in 0 ..2{
    e[t]=ba[t];
}
let i:u16=  u16::from_le_bytes(e);
return i; 
}
pub fn u16__TO__ByteArray(i:u16)->Vec<u8>{
// return i.to_ne_bytes().to_owned().unwrap();;
return i.to_ne_bytes() .iter().cloned().collect();
//×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
}

pub fn String_u16__TO__u16(s:String)->u16{ 
return  s.parse::<u16>().unwrap();
}
pub fn u16__TO__String(i:u16)->String{ 
//let mut s:String="".to_string();
let s=i.to_string();
return s;  
}
///////////////////


pub fn ByteArray__TO__u8(ba:Vec<u8>)->u8{
//ba.to_ne_bytes();
let mut e:[u8;1]=[0;1];
for t in 0 ..1{
    e[t]=ba[t];
}
let i:u8=  u8::from_le_bytes(e);
return i; 
}
pub fn u8__TO__ByteArray(i:u8)->Vec<u8>{
// return i.to_ne_bytes().to_owned().unwrap();;
return i.to_ne_bytes() .iter().cloned().collect();
//×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
}

pub fn String_u8__TO__u8(s:String)->u8{ 
return  s.parse::<u8>().unwrap();
}
pub fn u8__TO__String(i:u8)->String{ 
//let mut s:String="".to_string();
let s=i.to_string();
return s;  
}




   //////////////end uuuuuuuuuuuuu
       /////////////
   pub fn ByteArray__TO__f32(ba:Vec<u8>)->f32{
        //ba.to_ne_bytes();
       let mut e:[u8;4]=[0;4];
        for t in 0 ..4{
            e[t]=ba[t];
        }
       let i:f32=  f32::from_le_bytes(e);
       return i; 
      }
   pub fn f32__TO__ByteArray(i:f32)->Vec<u8>{
      // return i.to_ne_bytes().to_owned().unwrap();;
        return i.to_ne_bytes() .iter().cloned().collect();
        //×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
      }
    //
   pub fn String_f32__TO__f32(s:String)->f32{ 
        return  s.parse::<f32>().unwrap();
       }
   pub fn f32__TO__String(i:f32)->String{ 
        let s=i.to_string();
        return s;  
       }
     ///////////////////////
    pub fn ByteArray__TO__f64(ba:Vec<u8>)->f64{
        //ba.to_ne_bytes();
       let mut e:[u8;8]=[0;8];
        for t in 0 ..8{
            e[t]=ba[t];
        }
       let i:f64=  f64::from_le_bytes(e);
       return i; 
      }
    pub fn f64__TO__ByteArray(i:f64)->Vec<u8>{
      // return i.to_ne_bytes().to_owned().unwrap();;
        return i.to_ne_bytes() .iter().cloned().collect();
        //×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
      }
    //
   pub fn String_f64__TO__f64(s:String)->f64{ 
        return  s.parse::<f64>().unwrap();
       }
   pub fn f64__TO__String(i:f64)->String{ 
        let s=i.to_string();
        return s;  
       }
     ///////////////////////bool
    pub fn ByteArray__TO__bool(ba:Vec<u8>)->bool{
        //ba.to_ne_bytes();
       let mut e:[u8;1]=[0;1];
        for t in 0 ..1{
            e[t]=ba[t];
        }
       let mut i:bool= true; //bool::from_le_bytes(e);
       if e[0]==0 {i=false;};
       return i; 
      }
   pub fn bool__TO__ByteArray(i:bool)->Vec<u8>{
      // return i.to_ne_bytes().to_owned().unwrap();;
       let mut myv:Vec<u8>=Vec::with_capacity(1);
       myv=vec![0;1];
        if i==true {myv[0]=1;}

        return  myv;//i.to_ne_bytes() .iter().cloned().collect();
        //×¢Òâ  i.to_ne_bytes() ÊÇ [u8;4]  ÊÇ u8  array £¬
      }
    //
    pub fn String_bool__TO__bool(s:String)->bool{ 
        let mut myb=false;
        if s=="true"{myb=true;}
        return  myb;//s.parse::<bool>().unwrap();
       }
   pub fn bool__TO__String(i:bool)->String{ 

        let mut s:String="false".to_string();//i.to_string();
        if i==true{s="true".to_string();}
        return s;  
       } 
     ///////////end bool  
       
       
      
      ///////////////////////  
        
    } 
    
pub struct  drawBPH{
 
 
 }
 impl  drawBPH {
   
     pub fn drawBPH( hcanvas:&mut sdl2::render::Canvas<sdl2::video::Window>,hhba:Vec<u8>,hx:i32,hy:i32, hgap:i32,direct:i32) {
          //now drawBPH
        let mut hba:Vec<u8>=Vec::new();
        hba=BACV::String_utf8__TO__ByteArray("hello world".to_string());
        hba=BACV::i32__TO__ByteArray(65535);
        //hba=BACV::i32__TO__ByteArray(-1);
        hba=BACV::f32__TO__ByteArray(1.5);
        hba=hhba;
        let mut canvas:&mut sdl2::render::Canvas<sdl2::video::Window>=hcanvas;
        canvas.set_draw_color(Color::RGB(210, 210, 210)); 
        let mut x:i32=hx;//22 ;
        let mut y:i32=hy;//126; 
        let mut gap:i32=hgap;//5;

        let mut hb:u8=0;
        let mut hbb:u8=0;
       for k in 0 ..hba.len(){
        x=hx+gap*11*k as i32 ;
        hb=hba[k];
        //drawRect
        canvas.draw_rect(sdl2::rect::Rect::new(x,y,10*gap as u32,10*gap as u32));  //rect
        //draw  8 bit line:
        
        if direct==0{
        for j in 0 ..8{ 
          hbb=hb >>(7-j) & 1;
          if hbb>0 { 
              canvas.draw_line(((x+(j+1) as i32*gap) ,y+3*gap),(x+(j+1) as i32*gap,y+7*gap)); //long bit line
          }else{ 
              canvas.draw_line(((x+(j+1) as i32*gap) ,y+4*gap),(x+(j+1) as i32*gap,y+6*gap));  //short bit line
          }
         
         }
        }else if direct==1{
          for j in 0 ..8{ 
            hbb=hb >>(7-j) & 1;
            if hbb>0 { 
                canvas.draw_line((x+3*gap,((y+(j+1) as i32*gap) )),(x+7*gap,(y+(j+1) as i32*gap))); //long bit line
            }else{ 
                canvas.draw_line((x+4*gap,((y+(j+1) as i32*gap) )),(x+6*gap,(y+(j+1) as i32*gap))); //short bit line
            }
           
           }

        }else if direct==2{
          for j in 0 ..8{ 
            hbb=hb >>(j) & 1;
            if hbb>0 { 
                canvas.draw_line(((x+(j+1) as i32*gap) ,y+3*gap),(x+(j+1) as i32*gap,y+7*gap)); //long bit line
            }else{ 
                canvas.draw_line(((x+(j+1) as i32*gap) ,y+4*gap),(x+(j+1) as i32*gap,y+6*gap));  //short bit line
            }
           
           }

        }else if direct>=3{
          for j in 0 ..8{ 
            hbb=hb >>(j) & 1;
            if hbb>0 { 
                canvas.draw_line((x+3*gap,((y+(j+1) as i32*gap) )),(x+7*gap,(y+(j+1) as i32*gap))); //long bit line
            }else{ 
                canvas.draw_line((x+4*gap,((y+(j+1) as i32*gap) )),(x+6*gap,(y+(j+1) as i32*gap))); //short bit line
            }
           
           }


        }
         //draw  L:

        if direct==0{
         canvas.draw_line(((x+(1) as i32*gap) ,y+8*gap),((x+(1) as i32*gap) ,y+9*gap));//short L part, mark the b7, 
         canvas.draw_line(((x+(1) as i32*gap) ,y+9*gap),((x+(8.5*gap as f32) as i32) as i32,y+9*gap));// long L part
        }else if direct==1{
          canvas.draw_line(((x+(1) as i32*gap) ,y+1*gap),((x+(2) as i32*gap) ,y+1*gap));//short L part, mark the b7, 
          canvas.draw_line(((x+(1) as i32*gap) ,y+1*gap),((x+(1*gap ) as i32) as i32,(y+(8.5*gap as f32) as i32)));// long L part



        }else if direct==2{
          canvas.draw_line(((x+(8) as i32*gap) ,y+1*gap),((x+(8) as i32*gap) ,y+2*gap));//short L part, mark the b7, 
          canvas.draw_line(( (x+(0.6*gap as f32) as i32) as i32   ,y+1*gap),( (x+(8) as i32*gap),y+1*gap));// long L part

        }else if direct>=3{
          canvas.draw_line(((x+(8) as i32*gap) ,y+8*gap),((x+(9) as i32*gap) ,y+8*gap));//short L part, mark the b7, 
          canvas.draw_line(((x+(9) as i32*gap) ,y+(0.6*gap as f32)as i32),((x+(9*gap ) as i32) as i32,(y+(8*gap) as i32)));// long L part


          
        }
         //casted_ascii_char photo :
          //need  lib : sdl_ttf , SDL cannot show char photo  directly.
          //ttf: True Type Font   , ttf is Windows defalt font,
          
       }//end for 
     }//end drawBPH

    }
      fn main(){
         /// 
         print!("{}","\ntest BPH draw:::::::::::::::::::::: \n");
         let sdl2_context = sdl2::init().unwrap();
         let video = sdl2_context.video().unwrap();
         let mut window = video.window("drawBPH in sdl2", 800, 900)
             .position_centered()
             .opengl()
             .build()
             .unwrap();

         // window.set_title(" float,  1.5 , little endian");
         let mut canvas = window.into_canvas().build().unwrap();// window.renderer().accelerated().build().unwrap();
         canvas.set_draw_color(Color::RGB(110, 110, 110));
         canvas.clear();
       
/* 
         //now drawBPH
        let mut hba:Vec<u8>=Vec::new();
         hba=BACV::String_utf8__TO__ByteArray("hello world".to_string());
         hba=BACV::i32__TO__ByteArray(65535);
         //hba=BACV::i32__TO__ByteArray(-1);
         hba=BACV::f32__TO__ByteArray(1.5);
         canvas.set_draw_color(Color::RGB(210, 210, 210)); 
         let mut x:i32=22 ;
         let mut y:i32=126; 
         let mut gap:i32=5;

         let mut hb:u8=0;
         let mut hbb:u8=0;
        for k in 0 ..hba.len(){
         x+=gap*11 ;
         hb=hba[k];
         //drawRect
         canvas.draw_rect(sdl2::rect::Rect::new(x,y,10*gap as u32,10*gap as u32));  //rect
         //draw  8 bit line:
          for j in 0 ..8{ 
           hbb=hb >>(7-j) & 1;
           if hbb>0 { 
               canvas.draw_line(((x+(j+1) as i32*gap) ,y+3*gap),(x+(j+1) as i32*gap,y+7*gap)); //long bit line
           }else{ 
               canvas.draw_line(((x+(j+1) as i32*gap) ,y+4*gap),(x+(j+1) as i32*gap,y+6*gap));  //short bit line
           }
          
          }
          //draw  L:
          canvas.draw_line(((x+(1) as i32*gap) ,y+8*gap),((x+(1) as i32*gap) ,y+9*gap));//short L part, mark the b7, 
          canvas.draw_line(((x+(1) as i32*gap) ,y+9*gap),((x+(7.5*gap as f32) as i32) as i32,y+9*gap));// long L part
       
          //casted_ascii_char photo :
          //need  lib : sdl_ttf , SDL cannot show char photo  directly.
          //ttf: True Type Font   , ttf is Windows defalt font,

        }//end for 
        //end drawBPH
       */  

        let mut hba:Vec<u8>=Vec::new();
        //hba=BACV::String_utf8__TO__ByteArray("hello world".to_string());
        hba=BACV::i32__TO__ByteArray(65535);
        //hba=BACV::i32__TO__ByteArray(-1);
        //hba=BACV::f32__TO__ByteArray(1.5);
        canvas.set_draw_color(Color::RGB(210, 210, 210)); 

         let mut x=1;
         let mut y=1;
         hba=BACV::String_utf8__TO__ByteArray("hello world".to_string());
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         hba=BACV::String_utf8__TO__ByteArray("ŒÆËã".to_string());
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         hba=BACV::i32__TO__ByteArray(65535);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         hba=BACV::i32__TO__ByteArray(-1);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         
         hba=BACV::i64__TO__ByteArray(256*256*256*256-1);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         
         hba=BACV::i8__TO__ByteArray(127);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);


         hba=BACV::i8__TO__ByteArray(-128);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         y+=5*11;

         hba=BACV::f32__TO__ByteArray(1.5);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         hba=BACV::f32__TO__ByteArray(1.5);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,2);


         hba=BACV::f64__TO__ByteArray(1.5);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         
         hba=BACV::f64__TO__ByteArray(1.5);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,2);


         y+=5*11;

         hba=BACV::u32__TO__ByteArray(4294967295);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         hba=BACV::u8__TO__ByteArray(255);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,0);

         hba=BACV::u64__TO__ByteArray(4294967296*4294967295+4294967295);
         y+=5*11;
         drawBPH::drawBPH(&mut canvas,hba,x,y,5,1);

         //
         canvas.present();
         thread::sleep(Duration::from_millis(20000));
     
     
         /// 
         /// 
         /// 
       println!("\n{}","https://gitee.com/zzchinahebei/drawBPH");   
    }
